async function Fetch(url, method = 'GET', data = {}, onResponse = () => {}) {
    method = method.toUpperCase();

    const options = {
        method,
        headers: {
            'Content-Type': 'application/json',
        },
    };

    if (method !== 'GET') {
        options.body = JSON.stringify(data);
    }else{
        if (Object.keys(data).length > 0) {
            const query = new URLSearchParams(data).toString();
            url += (url.includes('?') ? '&' : '?') + query;
        }
    }

    try {
        const response = await fetch(url, options);
        const json = await response.json();
        onResponse(json);
        return json.data

    } catch (error) {
        const json = {
            status: 'fail',
            msg: '网络请求失败',
            data: {},
        };
        onResponse(json);
        return json.data
    }
}

function Redirect(url) {
    if (url) {
        window.location.href = url;
    } else {
        window.location.reload();
    }
}

function Event_(id,event_,lambda){
    let element  =document.getElementById(id);
    if (element) {
        element.addEventListener(event_, function(event) {
            event.preventDefault();
            lambda(event);
        });
    }
}

function Every(lambda, interval) {
    lambda();
    setInterval(lambda, interval);
}


function Pack(idMapping) {
    const data = {};

    Object.keys(idMapping).forEach(id => {
        const element = document.getElementById(id);
        if (element) {
            const fieldName = idMapping[id];
            data[fieldName] = element.value;
        }
    });

    return data;
}

